<template>
	<view class="hot-products-container">
		<!-- 统计卡片 -->
		<view class="stats-card">
			<view class="stats-header">
				<text class="stats-title">热销商品统计</text>
				<text class="stats-period">本月数据</text>
			</view>
			<view class="stats-grid">
				<view class="stat-item">
					<text class="stat-number">{{ totalSales }}</text>
					<text class="stat-label">总销量</text>
				</view>
				<view class="stat-item">
					<text class="stat-number">{{ totalAmount }}</text>
					<text class="stat-label">总销售额</text>
				</view>
				<view class="stat-item">
					<text class="stat-number">{{ avgPrice }}</text>
					<text class="stat-label">平均单价</text>
				</view>
			</view>
		</view>



		<!-- 排行榜 -->
		<view class="ranking-card">
			<view class="card-header">
				<text class="card-title">热销商品Top10</text>
				<view class="filter-tabs">
					<view class="filter-tab" :class="{ active: currentFilter === 'sales' }"
						@click="switchFilter('sales')">
						<text class="tab-text">销量</text>
					</view>
					<view class="filter-tab" :class="{ active: currentFilter === 'amount' }"
						@click="switchFilter('amount')">
						<text class="tab-text">销售额</text>
					</view>
				</view>
			</view>

			<view class="ranking-list">
				<view class="ranking-item" v-for="(product, index) in productList" :key="index"
					@click="viewProduct(product)">
					<view class="ranking-number" :class="[
						index === 0 ? 'rank-first' : '',
						index === 1 ? 'rank-second' : '',
						index === 2 ? 'rank-third' : '',
						index > 2 ? 'rank-normal' : ''
					]">
						<text class="number-text">{{ index + 1 }}</text>
					</view>
					<image :src="$tools.buildUrl(product.main_image)" class="product-image" mode="aspectFill" />
					<view class="product-info">
						<text class="product-name">{{ product.spu_name }}</text>
						<text class="product-spec">{{ product.sku_name }}</text>
						<view class="product-price">
							<text class="price-label">¥ {{ product.actual_price }}</text>
							<!-- <text class="profit-rate">利润率{{ product.profitRate }}%</text> -->
						</view>
					</view>
					<view class="product-stats">
						<view class="stat-row">
							<text class="stat-label">销量:</text>
							<text class="stat-value">{{ product.quantity }}</text>
						</view>
						<view class="stat-row">
							<text class="stat-label">销售额:</text>
							<text class="stat-value">¥{{ product.total_amount }}</text>
						</view>
						<!-- <view class="trend-indicator" :class="[
							product.trend === 'up' ? 'trend-up' : '',
							product.trend === 'down' ? 'trend-down' : '',
							product.trend === 'stable' ? 'trend-stable' : ''
						]">
							<text class="trend-text">{{ getTrendText(product.trend) }}</text>
						</view> -->
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 统计数据
				totalSales: '0',
				totalAmount: '0',
				avgPrice: '0',
				// 当前筛选
				currentFilter: 'sales',
				// 显示的商品列表
				productList: []
			}
		},
		onLoad() {
			this.loadProductList()
		},
		methods: {
			// 加载商品列表
			loadProductList() {
				this.productList = []
				this.$api.fenxiao.HotProductRanking({
					page: 1,
					limit: 10,
					sortType: this.currentFilter
				}).then(res => {
					this.productList = res.data
					this.totalSales = res.data.reduce((accumulator, currentValue) => {
						return accumulator + currentValue.quantity;
					}, 0);
					this.totalAmount = res.data.reduce((accumulator, currentValue) => {
						return accumulator + currentValue.total_amount;
					}, 0);
					if (this.totalSales > 0) {
						this.avgPrice = this.totalAmount / this.totalSales
					} else {
						this.avgPrice = 0
					}
				})
			},

			// 切换筛选条件
			switchFilter(filter) {
				this.currentFilter = filter
				this.loadProductList()
			},

			// 获取趋势文本
			getTrendText(trend) {
				const trendMap = {
					'up': '↗',
					'down': '↘',
					'stable': '→'
				}
				return trendMap[trend] || '→'
			},

			// 查看商品详情
			viewProduct(product) {
				uni.navigateTo({
					url: `/pages/product/detail?id=${product.spu_id}`
				})
			},


		}
	}
</script>

<style lang="scss" scoped>
	.hot-products-container {
		background-color: #f8f9fa;
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
	}

	/* 统计卡片 */
	.stats-card {
		background-color: white;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
	}

	.stats-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.stats-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	.stats-period {
		font-size: 24rpx;
		color: #999;
	}

	.stats-grid {
		display: flex;
		justify-content: space-around;
	}

	.stat-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.stat-number {
		font-size: 36rpx;
		font-weight: bold;
		color: #52a58a;
		margin-bottom: 8rpx;
	}

	.stat-label {
		font-size: 24rpx;
		color: #666;
	}


	/* 筛选标签 */
	.filter-tabs {
		display: flex;
		background-color: #f8f9fa;
		border-radius: 20rpx;
		padding: 4rpx;
		flex-wrap: nowrap;
	}

	.filter-tab {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 12rpx 20rpx;
		border-radius: 16rpx;
		transition: all 0.3s ease;
		white-space: nowrap;
		min-width: 0;
	}

	.filter-tab.active {
		background-color: #52a58a;
	}

	.tab-text {
		font-size: 24rpx;
		color: #666;
		white-space: nowrap;
	}

	.filter-tab.active .tab-text {
		color: white;
	}

	/* 排行榜 */
	.ranking-card {
		background-color: white;
		border-radius: 16rpx;
		padding: 30rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
	}

	.card-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.card-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	.ranking-list {
		margin-top: 20rpx;
	}

	.ranking-item {
		display: flex;
		align-items: center;
		padding: 20rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.ranking-item:last-child {
		border-bottom: none;
	}

	.ranking-number {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 20rpx;
		font-weight: bold;
	}

	.rank-first {
		background-color: #ffd700;
		color: white;
	}

	.rank-second {
		background-color: #c0c0c0;
		color: white;
	}

	.rank-third {
		background-color: #cd7f32;
		color: white;
	}

	.rank-normal {
		background-color: #f8f9fa;
		color: #666;
	}

	.number-text {
		font-size: 24rpx;
	}

	.product-image {
		width: 100rpx;
		height: 100rpx;
		border-radius: 12rpx;
		margin-right: 20rpx;
	}

	.product-info {
		flex: 1;
		margin-right: 20rpx;
	}

	.product-name {
		font-size: 28rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 8rpx;
		display: block;
	}

	.product-spec {
		font-size: 24rpx;
		color: #999;
		margin-bottom: 12rpx;
	}

	.product-price {
		display: flex;
		align-items: center;
	}

	.price-label {
		font-size: 26rpx;
		color: #52a58a;
		font-weight: bold;
		margin-right: 20rpx;
	}

	.profit-rate {
		font-size: 22rpx;
		color: #ff6b35;
		background-color: #fff5f0;
		padding: 4rpx 8rpx;
		border-radius: 8rpx;
	}

	.product-stats {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
		min-width: 160rpx;
	}

	.stat-row {
		display: flex;
		align-items: center;
		margin-bottom: 8rpx;
	}

	.stat-label {
		font-size: 22rpx;
		color: #999;
		margin-right: 8rpx;
	}

	.stat-value {
		font-size: 24rpx;
		color: #333;
		font-weight: bold;
	}

	.trend-indicator {
		padding: 4rpx 12rpx;
		border-radius: 12rpx;
		font-size: 20rpx;
		font-weight: bold;
	}

	.trend-indicator.trend-up {
		background-color: #d4edda;
		color: #155724;
	}

	.trend-indicator.trend-down {
		background-color: #f8d7da;
		color: #721c24;
	}

	.trend-indicator.trend-stable {
		background-color: #fff3cd;
		color: #856404;
	}

	.trend-text {
		font-size: 20rpx;
	}
</style>